// 标记管理
// hooks/useMarkers.ts
import { useEffect } from 'react';

type MarkerOptions = {
  position: AMap.LngLat;
  content?: string;
  icon?: string;
  draggable?: boolean;
};

export const useMarkers = (
  map: AMap.Map | null,
  markers: MarkerOptions[],
  onMarkerClick?: (marker: AMap.Marker) => void
) => {
  useEffect(() => {
    if (!map) return;

    const markerInstances: AMap.Marker[] = [];

    markers.forEach((options) => {
      const marker = new AMap.Marker({
        position: options.position,
        content: options.content,
        icon: options.icon,
        draggable: options.draggable,
      });

      if (onMarkerClick) {
        marker.on('click', () => onMarkerClick(marker));
      }

      markerInstances.push(marker);
      map.add(marker);
    });

    return () => {
      markerInstances.forEach((marker) => map.remove(marker));
    };
  }, [map, markers, onMarkerClick]);
};